package com.donovosoft.api.common;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.donovosoft.api.Bean;
import com.donovosoft.api.exceptions.InvalidXml;

public abstract class Rss implements Bean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4376880743715555861L;
	private String url;
	private Element root;

	public String getUrl() {
		return url;
	}

	public Element getRoot() {
		return root;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 
	 */
	public void init() {
		try {
			URL feed = new URL(this.url);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dbf.newDocumentBuilder();
			Document doc = builder.parse(new InputSource(feed.openStream()));
			this.root = (Element) doc.getDocumentElement();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return String
	 */
	public abstract String build() throws InvalidXml;

	/**
	 * 
	 * @param tag
	 *            String
	 * @return NodeList
	 */
	public abstract NodeList search(String tag) throws InvalidXml;
}
